package it.cums.core.util;

import it.cums.core.Activator;
import it.cums.core.model.Product;
import it.cums.core.model.Products;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class ProductManager {
	private static ProductManager productManager;
	
	private ProductManager() {
	}
	
	public static ProductManager getInstance() {
		if (productManager == null) {
			productManager = new ProductManager();
		}
		return productManager;
	}
	
	public void createAndStoreProduct(Products products, String name, int quantity, double purchasingPrice, int iva, String manufacturer, double salesPrice, String notes) throws Exception {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			Product p = new Product();
			p.setName(name);
			p.setAvailableQuantity(quantity);
			p.setPurchasingPrice(purchasingPrice);
			p.setIva(iva);
			p.setManufacturer(manufacturer);
			p.setSalesPrice(salesPrice);
			p.setNotes(notes);
			session.save(p);
			tx.commit();
			products.addProduct(p);
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Product can't be added to the database!");
			HibernateUtil.rollback(tx);
			throw e;
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	public void updateProduct(Product product, String name, int quantity, double purchasingPrice, int iva, String manufacturer, double salesPrice, String notes) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			product.setName(name);
			product.setAvailableQuantity(quantity);
			product.setPurchasingPrice(purchasingPrice);
			product.setIva(iva);
			product.setManufacturer(manufacturer);
			product.setSalesPrice(salesPrice);
			product.setNotes(notes);
			session.update(product);
			tx.commit();
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Product can't be updated in the database!");
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> listProducts() {
		Session session = null;
		Transaction tx = null;
		List<Product> products = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
		    tx = session.beginTransaction();
		    products = session.createQuery("from Product").list();
		    tx.commit();
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
		return products;
    }
	
	public boolean deleteProduct(Products products, Product product) {
		boolean successful = false;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.delete(product);
			tx.commit();
			products.removeProduct(product);
			successful = true;
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Product '" + product.getName() + "' can't be removed from the database!");
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
		return successful;
	}
}